/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @typedef {{val: number, next: ListNode?}} ListNode
 */

/**
 * 141. 环形链表
 * @link https://leetcode-cn.com/problems/linked-list-cycle/description/
 * 给定一个链表头节点 head，判断链表中是否有环
 *
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
    let p1 = head;
    let p2 = head && head.next;

    while (p1 && p2) {
        if (p1 === p2) {
            return true;
        }

        p1 = p1.next;
        p2 = p2.next && p2.next.next;
    }

    return false;
};
